package com.szxy.service;

import com.szxy.conf.MyMessageSource;
import org.apache.rocketmq.common.message.MessageConst;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageHeaders;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Service;
import org.springframework.util.MimeTypeUtils;

/**
 * @Author: zwz
 * @Date:2020/7/16 21:45
 * @Description:com.szxy.service
 * @Version:1.0
 **/
@Service
public class MyMessageService {

    @Autowired
    private MyMessageSource mqSource;

    //1. 发送普通文本消息
    public void send(String msg){
        Message<String> message = MessageBuilder.withPayload(msg)
                .build();
        this.mqSource.output1().send(message);
    }
    //2. 发送带有标签 tag 消息
    public void send(String msg, String tag){
        Message<String> message = MessageBuilder.withPayload(msg)
                .setHeader(MessageConst.PROPERTY_TAGS, tag)
                .build();
        this.mqSource.output1().send(message);
    }
    //3. 发送对象消息
    public <T> void send(T msg, String tag){
        Message<T> message = MessageBuilder.withPayload(msg)
                .setHeader(MessageConst.PROPERTY_TAGS, tag)
                .setHeader(MessageHeaders.CONTENT_TYPE, MimeTypeUtils.APPLICATION_JSON)
                .build();
        this.mqSource.output1().send(message);
    }
    //4. 发送事务消息
    public <T> void sendTransMsg(T msg, int num){
        Message<T> message = MessageBuilder.withPayload(msg)
                .setHeader("test", String.valueOf(num))
                .setHeader(MessageHeaders.CONTENT_TYPE, MimeTypeUtils.APPLICATION_JSON)
                .build();
        this.mqSource.output2().send(message);
    }

    //5. 发送可手动 pull 拉取消息
    public void sendMassiveMessage(String msg){
        this.mqSource.output3().send(MessageBuilder.withPayload(msg).build());
    }



}
